import { ref, computed } from 'vue';
import { defineStore } from 'pinia';
// 直接拿json文件获取路由的配置信息，相当于后台管理里面拿meta信息
import { pages, subPackages } from '@/pages.json';

export const useRouteStore = defineStore('__route__', () => {
    // 路由跳转白名单
    const whiteList = [
        '',
    ];
    // 登录页面路由， 因为要做路由返回
    const loginRoute = [
        '/pages-sub/login/index',
    ]

    // 是否处于跳转状态，解决分包跳转超时问题
    const jumping = ref<{
        timer: number | null,
        page: string,
    }>({
        timer: null,
        page: '',
    });

    // 获取当前路由
    const getCurrentRoute = () => {
        const pages = getCurrentPages();
        return pages[pages.length - 1].route;
    }

    // 是否缓存页面，因为分包页面直接调用的时候会出现timeout的情况
    const cachePages = ref<string[]>([]);
    // 添加缓存页面
    const addCachePage = (page: string) => {
        const purePageURL = getPurePageURL(page);
        if(!cachePages.value.includes(purePageURL)) {
            cachePages.value.push(purePageURL);
        }
    }
    // 移除缓存页面
    const removeCachePage = (page: string) => {
        const purePageURL = getPurePageURL(page);
        cachePages.value = cachePages.value.filter(p => p !== purePageURL);
    }

    // 加載分包页面的时候慢200毫秒
    const slowLoadSubPackagePage = (page: string) => {
        return new Promise((resolve, reject) => {
            if(!jumping.value.timer) {
                jumping.value.page = page;
                jumping.value.timer = setTimeout(() => {
                    jumping.value.timer = null;
                    jumping.value.page = '';
                    resolve(true);
                }, 200);
            } else {
                resolve(true);
            }
        });
    }

    // 获取纯净的页面url
    const getPurePageURL = (url: string) => {
        let purePageURL = url.split('?')[0];
        // 如果纯净的页面url以/开头，则去掉/
        if(!purePageURL.startsWith('/')) {
            purePageURL = '/' + purePageURL;
        }
        return purePageURL;
    }

    // 分包路由
    const subPackageRoots = subPackages.map((subPackage: any) => subPackage.root);
    // 判斷是否为分包路由
    const isSubPackage = (url: string) => {
        const purePageURL = getPurePageURL(url);
        return subPackageRoots.find(page => {
            if (purePageURL.startsWith('/' + page)) {
                return true;
            }
            return false;
        });
    }

    // 当前系统所有路由
    const allRoutes = computed(() => {
        const mainPages = pages.map((page: any) => {
            return {
                ...page,
                path: '/' + page.path,
            }
        });
        const subPages: any[] = [];
        subPackages.forEach((subPackage: any) => {
            subPackage.pages.forEach((page: any) => {
                subPages.push({
                    ...page,
                    path: '/' + subPackage.root + '/' + page.path,
                })
            })
        })
        return [...mainPages, ...subPages];
    });

    // 当前路由信息
    const currentRouteInfo = computed(() => {
        const currentRoute = getCurrentRoute();
        const purePageURL = getPurePageURL(currentRoute || '');
        return getRouteInfo(purePageURL || '');
    })

    // 获取当前路由的信息
    const getRouteInfo = (url: string) => {
        const purePageURL = getPurePageURL(url);
        return allRoutes.value.find(route => route.path === purePageURL);
    }

    return {
        whiteList,
        loginRoute,
        jumping,
        getCurrentRoute,
        cachePages,
        addCachePage,
        removeCachePage,
        slowLoadSubPackagePage,
        isSubPackage,
        allRoutes,
        getRouteInfo,
        currentRouteInfo,
    }
})